package com.ruoyi.xqjh.lhxxqjh.controller;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.EasyExcelUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.xqjh.lhxxqjh.domain.Lhxxqjh;
import com.ruoyi.xqjh.lhxxqjh.domain.LhxxqjhMx;
import com.ruoyi.xqjh.lhxxqjh.service.ILhxxqjhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;

/**
 * 6号线需求计划Controller
 *
 * @author huacai
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/xqjh/lhxxqjh")
public class LhxxqjhController extends BaseController {
    @Autowired
    private ILhxxqjhService lhxxqjhService;

    @Autowired
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询6号线需求计划列表
     */
    @PreAuthorize("@ss.hasPermi('xqjh:lhxxqjh:list')")
    @GetMapping("/list")
    public TableDataInfo list(Lhxxqjh lhxxqjh) {
        startPage();
        List<Lhxxqjh> list = lhxxqjhService.selectLhxxqjhList(lhxxqjh);
        return getDataTable(list);
    }

    /**
     * 导出6号线需求计划列表
     */
    @PreAuthorize("@ss.hasPermi('xqjh:lhxxqjh:export')")
    @Log(title = "6号线需求计划" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Lhxxqjh lhxxqjh) {
        List<Lhxxqjh> list = lhxxqjhService.selectLhxxqjhList(lhxxqjh);
        ExcelUtil<Lhxxqjh> util = new ExcelUtil<Lhxxqjh>(Lhxxqjh.class);
        util.exportExcel(response, list, "6号线需求计划数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Lhxxqjh> util = new ExcelUtil<Lhxxqjh>(Lhxxqjh.class);
        util.importTemplateExcel(response, "6号线需求计划数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "6号线需求计划" , businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('xqjh:lhxxqjh:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Lhxxqjh> util = new ExcelUtil<Lhxxqjh>(Lhxxqjh.class);
        InputStream inputStream = file.getInputStream();
        List<Lhxxqjh> list = util.importExcel(inputStream);
        inputStream.close();
        int count = lhxxqjhService.batchInsertLhxxqjh(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取6号线需求计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('xqjh:lhxxqjh:query')")
    @GetMapping(value = "/{lhxxqjhId}")
    public AjaxResult getInfo(@PathVariable("lhxxqjhId") String lhxxqjhId) {
        return success(lhxxqjhService.selectLhxxqjhByLhxxqjhId(lhxxqjhId));
    }

    /**
     * 新增6号线需求计划
     */
    @PreAuthorize("@ss.hasPermi('xqjh:lhxxqjh:add')")
    @Log(title = "6号线需求计划" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Lhxxqjh lhxxqjh) {
        int insertLhxxqjh = lhxxqjhService.insertLhxxqjh(lhxxqjh);

        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(lhxxqjh.getLhxxqjhId());
        infoRunTaskData.setBusinessType(15); //业务模块: 6号线需求计划
        infoRunTaskData.setBusinessTitle("6号线需求计划"); //任务名称: 6号线需求计划
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人
        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(insertLhxxqjh);
    }

    /**
     * 修改6号线需求计划
     */
    @PreAuthorize("@ss.hasPermi('xqjh:lhxxqjh:edit')")
    @Log(title = "6号线需求计划" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Lhxxqjh lhxxqjh) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(lhxxqjh.getLhxxqjhId());
        infoRunTaskData.setBusinessType(15); //业务模块: 6号线需求计划
        infoRunTaskData.setBusinessTitle("6号线需求计划"); //任务名称: 6号线需求计划
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData);

        return toAjax(lhxxqjhService.updateLhxxqjh(lhxxqjh));
    }

    /**
     * 删除6号线需求计划
     */
    @PreAuthorize("@ss.hasPermi('xqjh:lhxxqjh:remove')")
    @Log(title = "6号线需求计划" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{lhxxqjhIds}")
    public AjaxResult remove(@PathVariable String[] lhxxqjhIds) {
        //同时删除业务关系数据
        for (String lhxxqjhId : lhxxqjhIds) {
            Lhxxqjh lhxxqjh = lhxxqjhService.selectLhxxqjhByLhxxqjhId(lhxxqjhId);
            iInfoRunTaskDataService.deleteData(lhxxqjh.getLhxxqjhId(), 15);//业务模块: 人才平台-人员申请
        }
        return toAjax(lhxxqjhService.deleteLhxxqjhByLhxxqjhIds(lhxxqjhIds));
    }

    /**
     * 使用EasyExcel根据模板导出excel
     */
    @PostMapping("/exportWithTemplates")
    public void exportWithTemplate(HttpServletResponse response, Lhxxqjh lhxxqjh) {
        Date createTimeByDate = lhxxqjh.getCreateTime();
        String createTime = DateUtil.format(createTimeByDate, "yyyy-MM-dd");

        String lhxxqjhId = lhxxqjh.getLhxxqjhId();
        List<LhxxqjhMx> lhxxqjhMxList = lhxxqjhService.selectLhxxqjhByLhxxqjhId(lhxxqjhId).getLhxxqjhMxList();

        for (int i = 0; i < lhxxqjhMxList.size(); i++) {
            lhxxqjhMxList.get(i).setNum(i + 1);
        }

        for (LhxxqjhMx lhxxqjhMx : lhxxqjhMxList) {
            Date sySj = lhxxqjhMx.getSySj();
            String newSySj = DateUtil.format(sySj, "yyyy-MM-dd");
            lhxxqjhMx.setNewSySj(newSySj);
        }

        BigDecimal totalSl = lhxxqjhMxList.stream()
                .map(LhxxqjhMx::getSl)  // 假设 getSl() 是获取 sl 字段的方法
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        // 准备导出数据
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("createTime" , createTime);
        dataMap.put("list", lhxxqjhMxList);
        dataMap.put("totalSl", totalSl);
        // 调用导出工具
        EasyExcelUtil.exportWithTemplate(response, dataMap, "6hxxqjh.xlsx" , "6号线需求计划");
    }
}
